<?php

namespace Database\Factories;

use App\Models\ModelNumberSpecification;
use App\Models\Order;
use App\Models\Product;
use App\Rep\OrderRep;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Collection;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Order>
 */
class OrderFactory extends Factory
{

    /**
     * 虚拟订单添加方式：
     * php artisan tinker
     * \App\Models\Order::factory()->count(1000)->create();
     */

    protected $model = Order::class;

    /**
     * Define the model's default state.
     * 定义模型的默认状态
     *
     * @return array<string, mixed>
     */
    public function definition()
    {


        $product_list = Product::where('is_del',2)->with(['color','modelNumber','jobType'])->get()->toArray();
        if(empty($product_list)){
            return [];
        }
        $key = array_rand($product_list);
        $product_id = $product_list[$key]['id'];
        //p($product_id);

        if(empty($product_list[$key]['model_number'])){
            return [];
        }
        //p($product_list[$key]['model_number']);
        $model_number_key = array_rand($product_list[$key]['model_number']);
        //p($model_number_key);
        $model_number_id = $product_list[$key]['model_number'][$model_number_key]['pivot']['model_number_id'];
        //p($model_number_id);
        $specification_list = ModelNumberSpecification::where('model_number_id',$model_number_id)->pluck('specification_id')->toArray();
        if(empty($specification_list)){
            return [];
        }
        $specification_id = $specification_list[array_rand($specification_list)];
        if(empty($product_list[$key]['color'])){
            return [];
        }
        $color_key = array_rand($product_list[$key]['color']);
        $color_id =  $product_list[$key]['color'][$color_key]['pivot']['color_id'];

        $arr = [
            //流水号
            'serial_number'=>"LK".date('YmdHis').fake()->unixTime,
            //订单号
            'order_number'=>fake()->unixTime,
            //订单时间
            'order_date'=>fake()->dateTimeBetween('2023-05-01','now','PRC'),
            //交货时间
            'delivery_date'=>fake()->dateTimeBetween('now','2023-12-31','PRC'),
            //业务员id
            'account_executive_id'=>16,
            //产品
            'product_id'=>$product_id,
            //型号
            'model_number_id'=>$model_number_id,
            //规格
            'specification_id'=>$specification_id,
            //颜色
            'color_id'=>$color_id,
            //方向
            'direction'=>fake()->randomElement(['上', '下', '左', '右']),
            //客户
            'customer_id'=>fake()->numberBetween(1, 5),
            //应付款
            'receivables'=>fake()->numberBetween(1000, 9999),
            //实付款
            'received'=>fake()->numberBetween(100, 999),
            //备注
            'remark'=>fake()->text(50),
            //紧急程度
            'emergency_procedure'=>fake()->numberBetween(1, 3),
            //是否补录订单
            'is_replenishment'=>2,
            //备货
            'is_stock_up'=>2,
            //添加人员
            'user_id'=>1,
            'batch_number'=>fake()->numberBetween(10000000, 99999999)

        ];
        return $arr;
    }


}
